import java.io.*;
import java.util.Scanner;
import org.newdawn.slick.*;
import org.newdawn.slick.util.FontUtils;
import org.newdawn.slick.command.*;

/**
 * Contains, saves, and loads settings.
 * 
 * @author Gerrit Jamerson, CodeMonkeys
 * @version 4/4/13
 */
public class Settings
{
    private static int intResX, intResY, intSFXVol, intBGMVol;
    private static int intKeyUp, intKeyDown, intKeyLeft, intKeyRight, intKeySelect, intKeyBack, intKeyAttack, intKeyDefend;
    private static Boolean isFullscreen;
    /**
     * Reads the settings from config.txt
     * 
     * @pre None
     * @param None
     * @return Void
     * @post Settings have been loaded
     */
    public static void loadSettings()
    {
        File settingsFile = new File("config.txt");
        if(!settingsFile.exists())
        {
            try
            {
                System.out.println("No settings file found, creating new one.");

                settingsFile.createNewFile();
                FileWriter fileWriter = new FileWriter(settingsFile);
                fileWriter.write("ResX=800\r\n");
                fileWriter.write("ResY=600\r\n");
                fileWriter.write("Fullscreen=false\r\n");
                fileWriter.write("SFXVol=100\r\n");
                fileWriter.write("BGMVol=100\r\n");
                fileWriter.write("keyUp=" + Input.KEY_UP + "\r\n");
                fileWriter.write("keyDown=" + Input.KEY_DOWN + "\r\n");
                fileWriter.write("keyLeft=" + Input.KEY_LEFT + "\r\n");
                fileWriter.write("keyRight=" + Input.KEY_RIGHT + "\r\n");
                fileWriter.write("keySelect=" + Input.KEY_ENTER + "\r\n");
                fileWriter.write("keyBack=" + Input.KEY_ESCAPE + "\r\n");
                fileWriter.write("keyAttack=" + Input.KEY_Z + "\r\n");
                fileWriter.write("keyDefend=" + Input.KEY_X + "\r\n");
                fileWriter.close();
            } //End try
            catch (IOException e)
            {
                System.out.println(e.getMessage());
            } //End catch

            loadSettings();
        } //End if file doesnt exist
        else
        {
            try
            {
                System.out.println("Settings file found, loading values...");
                FileReader fileReader = new FileReader(settingsFile);
                Scanner scanner = new Scanner(fileReader);

                String[] configValues = new String[13];
                for(int i = 0; i < configValues.length; i++)
                {
                    configValues[i] = scanner.nextLine();
                } //End for
                fileReader.close();

                intResX = Integer.parseInt(configValues[0].replace("ResX=", "").replace("\r\n", ""));
                intResY = Integer.parseInt(configValues[1].replace("ResY=", ""));
                isFullscreen = new Boolean(configValues[2].replace("Fullscreen=", ""));
                intSFXVol = Integer.parseInt(configValues[3].replace("SFXVol=", ""));
                intBGMVol = Integer.parseInt(configValues[4].replace("BGMVol=", ""));
                intKeyUp = Integer.parseInt(configValues[5].replace("keyUp=", ""));
                intKeyDown = Integer.parseInt(configValues[6].replace("keyDown=", ""));
                intKeyLeft = Integer.parseInt(configValues[7].replace("keyLeft=", ""));
                intKeyRight = Integer.parseInt(configValues[8].replace("keyRight=", ""));
                intKeySelect = Integer.parseInt(configValues[9].replace("keySelect=", ""));
                intKeyBack = Integer.parseInt(configValues[10].replace("keyBack=", ""));
                intKeyAttack = Integer.parseInt(configValues[11].replace("keyAttack=", ""));
                intKeyDefend = Integer.parseInt(configValues[12].replace("keyDefend=", ""));

                System.out.println("X Resolution set to " + intResX);
                System.out.println("Y Resolution set to " + intResY);
                System.out.println("Fullscreen set to " + isFullscreen);
                System.out.println("SFX Volume set to " + intSFXVol);
                System.out.println("BGM Volume set to " + intBGMVol);
            } //End try
            catch (IOException e)
            {
                System.out.println(e.getMessage());
            } //End catch
        } //End if file exists
    } //End loadSettings method

    /**
     * Writes the settings to config.txt
     * 
     * @pre None
     * @param None
     * @return Void
     * @post Settings have been saved
     */
    public static void saveSettings()
    {
        try
        {
            File settingsFile = new File("config.txt");
            settingsFile.delete();
            settingsFile.createNewFile();
            FileWriter fileWriter = new FileWriter(settingsFile);
            fileWriter.write("ResX="+intResX+"\r\n");
            fileWriter.write("ResY="+intResY+"\r\n");
            fileWriter.write("Fullscreen="+isFullscreen+"\r\n");
            fileWriter.write("SFXVol="+intSFXVol+"\r\n");
            fileWriter.write("BGMVol="+intBGMVol+"\r\n");
            fileWriter.write("keyUp=" + intKeyUp + "\r\n");
            fileWriter.write("keyDown=" + intKeyDown + "\r\n");
            fileWriter.write("keyLeft=" + intKeyLeft + "\r\n");
            fileWriter.write("keyRight=" + intKeyRight + "\r\n");
            fileWriter.write("keySelect=" + intKeySelect + "\r\n");
            fileWriter.write("keyBack=" + intKeyBack + "\r\n");
            fileWriter.write("keyAttack=" + intKeyAttack + "\r\n");
            fileWriter.write("keyDefend=" + intKeyDefend + "\r\n");
            fileWriter.close();

            System.out.println("Settings have been saved.");
        } //End try
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        } //End catch
    } //End saveSettings method

    /**
     * Returns the X resolution loaded from the config.
     * 
     * @pre Must have called loadSettings()
     * @param None
     * @return The resolution (x)
     * @post None
     */
    public static int getResolutionX()
    {
        return intResX;
    } //End getResolutionX method

    /**
     * Returns the Y resolution loaded from the config.
     * 
     * @pre Must have called loadSettings()
     * @param None
     * @return The resolution (y)
     * @post None
     */
    public static int getResolutionY()
    {
        return intResY;
    } //End getResolutionY method

    /**
     * Returns the sound effects volume loaded from the config.
     * 
     * @pre Must have called loadSettings()
     * @param None
     * @return The volume (0 - 100)
     * @post None
     */
    public static int getSFXVolume()
    {
        return intSFXVol;
    } //End getSFXVolume method

    /**
     * Returns the background music volume loaded from the config.
     * 
     * @pre Must have called loadSettings()
     * @param None
     * @return The volume (0 - 100)
     * @post None
     */
    public static int getBGMVolume()
    {
        return intBGMVol;
    } //End getBGMVolume method

    /**
     * Sets the X resolution loaded from the config.
     * 
     * @pre Must have called loadSettings()
     * @param _intResX The X resolution
     * @return Void
     * @post None
     */
    public static void setResolutionX(int _intResX)
    {
        intResX = _intResX;
    } //End getResolutionX method

    /**
     * Sets the Y resolution loaded from the config.
     * 
     * @pre Must have called loadSettings()
     * @param _intResY The Y resolution
     * @return Void
     * @post None
     */
    public static void setResolutionY(int _intResY)
    {
        intResY = _intResY;
    } //End getResolutionY method

    /**
     * Sets the sound effects volume loaded from the config.
     * 
     * @pre Must have called loadSettings()
     * @param intVol The volume
     * @return Void
     * @post None
     */
    public static void setSFXVolume(int intVol)
    {
        intSFXVol = intVol;
    } //End getSFXVolume method

    /**
     * Sets the background music volume loaded from the config.
     * 
     * @pre Must have called loadSettings()
     * @param intVol The volume
     * @return Void
     * @post None
     */
    public static void setBGMVolume(int intVol)
    {
        intBGMVol = intVol;
    } //End getBGMVolume method

    /**
     * Sets whether the game should be full screen.
     * @param _isFullscreen Whether or not the window should be fullscreen.
     */
    public static void setFullscreen(boolean _isFullscreen)
    {
        isFullscreen = _isFullscreen;
    } //End setFullScreen method

    /**
     * Returns whether the game should be fullscreen
     * @return True if fullscreen
     */
    public static boolean getFullscreen()
    {
        return isFullscreen.booleanValue();
    } //End getFullscreen

    /**
     * Returns the key ID.
     * @return The keycode of the given key.
     */
    public static int getKeyUp()
    {
        return intKeyUp;
    } //End get key method

    /**
     * Returns the key ID.
     * @return The keycode of the given key.
     */
    public static int getKeyDown()
    {
        return intKeyDown;
    } //End get key method

    /**
     * Returns the key ID.
     * @return The keycode of the given key.
     */
    public static int getKeyLeft()
    {
        return intKeyLeft;
    } //End get key method

    /**
     * Returns the key ID.
     * @return The keycode of the given key.
     */
    public static int getKeyRight()
    {
        return intKeyRight;
    } //End get key method

    /**
     * Returns the key ID.
     * @return The keycode of the given key.
     */
    public static int getKeySelect()
    {
        return intKeySelect;
    } //End get key method

    /**
     * Returns the key ID.
     * @return The keycode of the given key.
     */
    public static int getKeyBack()
    {
        return intKeyBack;
    } //End get key method

    /**
     * Returns the key ID.
     * @return The keycode of the given key.
     */
    public static int getKeyAttack()
    {
        return intKeyAttack;
    } //End get key method

    /**
     * Returns the key ID.
     * @return The keycode of the given key.
     */
    public static int getKeyDefend()
    {
        return intKeyDefend;
    } //End get key method
    
    /**
     * Returns the ID of the given key.
     * @param key The key to get the ID.
     * @return the ID of the key.
     */
    public static int getKeyID(Keys key)
    {
        switch(key)
        {
            case UP:
            return intKeyUp;
            
            case DOWN:
            return intKeyDown;
            
            case LEFT:
            return intKeyLeft;
            
            case RIGHT:
            return intKeyRight;
            
            case SELECT:
            return intKeySelect;
            
            case BACK:
            return intKeyBack;
            
            case ATTACK:
            return intKeyAttack;
            
            case DEFEND:
            return intKeyDefend;
        } //End switch
        
        return -1;
    } //End getKeyID method
} //End class
